package com.boboPlanet.comm.rabbitmq.consume;

import com.boboPlanet.comm.context.BusinessThreadContextHolder;
import com.boboPlanet.comm.enums.PlanetCommCode;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;

/**
 * @Description: TODO ChannelAwareMessageListener 在消费之前做一些事，注入线程上下文信息,就不用在每个消费者中都解析了
 * @Author: mis_wu
 * @Date: 2023/6/22
 * @Company planet
 * MessageListener,
 * 目前没实现，不推荐用，使用aop
 **/
@Slf4j
@Component
public class ConsumeListenerHandle implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        log.info("into rabbitmq consume before,will operate something ...");
        //注入线程上下文信息
//        byte[] capture = (byte[]) message.getMessageProperties().getHeaders().get(PlanetCommCode.MQ_HEADER_CAPTURE.getCode());
//
//        ByteArrayInputStream bis = new ByteArrayInputStream(capture);
//
//        ObjectInput in = new ObjectInputStream(bis);
//
//        try {
//            //确保 BusinessThreadContextHolder 实现了序列化
//            BusinessThreadContextHolder holder = (BusinessThreadContextHolder) in.readObject();
//            holder.inject();
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        }finally {
//            BusinessThreadContextHolder.clear();
//        }
    }

}
